Automation Test Practice

Follow me on GitHub

Data Driven Test in pytest – parametization

Python is becoming the most popular programming language these days. The syntax is simple and expressive, there are tons of Python open source modules and frameworks available, and the community is welcoming and diverse. It’s also getting more and more widely used in Automation Test. Data Driven is essential for a same test case but different test data be executed repeatedly and automatically.

pytest to Python is similar with TestNG to JAVA, it not only is to be used to write small test, but also be scalable enough to support large test suite running.

Here I’d like to introduce a simple approach on how to use pytest parametrize function to import test data. This is similar with @DataProvider in TestNG.

Simple example:

pytest_1.png
Remember the variables name in parameterize method should be the same in the test method.

Challenge is that in real project, we normally store the test data in a seperate file. Thus we firstly need to read the test data into an array of tuples, the format like this [(), (), (),()]. Following is an example of reading test data from a csv file to an array of tuples.

ptest_2.png

Then in our test, we can use the test data in this way

pytest_3.png

Hope this is helpful for you. In next session, I’ll introduct another approach on Data Driven using pytest fixture feature.

Back To Homepage
Data Driven Test in pytest – fixture